package com.ebook.server.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ebook.core.domain.Order;
import com.ebook.core.domain.User;
import com.ebook.server.service.OrderService;
import com.ebook.util.Constant;

//TODO modify

@Controller
@RequestMapping(value = "/order")
public class OrderController {
	
	@Resource
	OrderService orderService;
	

	@RequestMapping(value = "/{orderid}.htm")
	public String showOrderInfo(@PathVariable int orderid, Model model, HttpServletRequest request, HttpServletResponse response) {

		Order order = orderService.getOrderById(orderid);
		model.addAttribute("order", order);
		
		return "/order/detail";
	}
	
	@RequestMapping(value = "/list")
	public String showOrders(Model model, HttpServletRequest request, HttpServletResponse response) {
		User user = (User)request.getSession().getAttribute(Constant.LOGIN_USER);
		int uid = -1;
		
		try {
			uid = Integer.valueOf(request.getParameter("uid"));
		} catch (NumberFormatException e) {
			uid = -1;
		}
		
		if (user != null && uid > 0 && user.getId() == uid) {
			List<Order> orders = orderService.getOrdersByUserId(uid);
			model.addAttribute("orders", orders);
		}

		
		return "/order/list";
	}
}
